﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace EstateNuke.Data
{
    public partial class Plan
    {
        #region Fileds & Properties

        /// <summary>
        /// </summary>
        public string DisplayName
        {
            get
            {
                return string.Format("{0}-{1}{2}", BeginDate.ToString("yyyy年MM月dd日"), EndDate.ToString("yyyy年MM月dd日"), IsDeleted ? " (已删除)" : "");
            }
        }

        /// <summary>
        /// </summary>
        public IList<EmployeePlan> EmployeePlans
        {
            get
            {
                return InternalEmployeePlans.Where(employeePlan => employeePlan.IsDeleted == false).ToList();
            }
        }

        #endregion

        #region Add & Remove EmployeePlan

        /// <summary>
        /// </summary>
        /// <returns></returns>
        public EmployeePlan AddEmployeePlan(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            var employeePlan = new EmployeePlan
                                   {
                                       RowId = Guid.NewGuid(),
                                       CreatedDate = DateTime.Now,
                                   };
            InternalEmployeePlans.Add(employeePlan);
            employeePlan.IsDeleted = false;
            employeePlan.ModifiedDate = DateTime.Now;
            employeePlan.ModifiedByUser = modifiedUser;
            employeePlan.SaleArea = 0;
            employeePlan.LeaseArea = 0;
            employeePlan.TotalArea = 0;
            return employeePlan;
        }

        /// <summary>
        /// </summary>
        /// <param name = "employeePlan"></param>
        /// <param name = "modifiedUser"></param>
        public void RemoveEmployeePlan(EmployeePlan employeePlan, User modifiedUser)
        {
            if (employeePlan == null)
            {
                throw new ArgumentNullException("employeePlan");
            }
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            if (InternalEmployeePlans.Contains(employeePlan))
            {
                employeePlan.Delete(modifiedUser);
            }
        }

        #endregion

        #region Ensure

        /// <summary>
        /// </summary>
        public void Ensure()
        {
            Comment = string.IsNullOrEmpty(Comment) ? null : Comment.Length > 512 ? Comment.Substring(0, 512) : Comment;
        }

        #endregion

        #region Delete

        /// <summary>
        /// </summary>
        public void Delete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = true;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
            ((List<EmployeePlan>) EmployeePlans).ForEach(employeePlan => employeePlan.Delete(modifiedUser));
        }

        /// <summary>
        /// </summary>
        public void CancelDelete(User modifiedUser)
        {
            if (modifiedUser == null)
            {
                throw new ArgumentNullException("modifiedUser");
            }
            IsDeleted = false;
            ModifiedDate = DateTime.Now;
            ModifiedByUser = modifiedUser;
        }

        #endregion
    }
}